import pygame
import random
import os

# 初始化 pygame
pygame.init()

# 窗口大小
WIDTH, HEIGHT = 804, 651

# 创建窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("哇~钢板")

# 加载图片
image_path = "1.1"
image = pygame.image.load(image_path)
image = pygame.transform.scale(image, (804, 651))  # 调整图片大小

# 图片显示位置
image_rect = image.get_rect(center=(WIDTH // 2, HEIGHT // 2))

# 加载声音文件
SOUND_DIR = "sounds"
sound_files = [os.path.join(SOUND_DIR, f) for f in os.listdir(SOUND_DIR) if f.endswith(('.2'))]
sounds = [pygame.mixer.Sound(sound_file) for sound_file in sound_files]

# 主循环
running = True
while running:
    screen.fill((255, 255, 255))  # 白色背景
    screen.blit(image, image_rect)  # 显示图片
    pygame.display.flip()  # 更新屏幕

    # 事件监听
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 获取点击位置
            mouse_pos = pygame.mouse.get_pos()
            if image_rect.collidepoint(mouse_pos):
                # 点击在图片区域内，播放随机声音
                if sounds:
                    random.choice(sounds).play()
                else:
                    print("没有可用的声音文件！")

# 退出 pygame
pygame.quit()
